﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RoleAssign.aspx.cs" Inherits="main_admin_RoleAssign" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>权限分配</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="../../css/style.css" rel="stylesheet" />
</head>
 
<body>

    <form id="form1" runat="server">    
	<input type="hidden" id="setfunc" name="setfunc" value="" runat="server"/>
	<input type="hidden" id="level" name="level"  value="" runat="server"/>
	
<div class="conter_right">
	<div class="sfkr"><span>当前位置：</span>角色管理</div>
    <fieldset class="dsadq"><legend style="color:#434449">权限分配</legend>
    	<table width="100%" border="0" cellpadding="0" cellspacing="0" class="msels">
					<tr >
						<td  valign="top" align="left">
                  			<div class="content_right_cont">
				  				<table width="100%" height="300px" border="0" cellspacing="0" align="center">
									<tr>
					  					<td valign="top"><span id="menus"></span></td>
									</tr>
				  				</table>
                  			</div>
						</td>
					</tr>
				</table>
                </fieldset>
</div>
<div style="height:15px"></div>
<div class="nstkaah" align="center" style="width:90%">
	<asp:Button runat="server" Text="确定" CssClass="seaal" id="Button2"   ToolTip="确定"  OnClientClick="return addAgentRoleFunc();" />
	<input type="submit" name="Button1" value="关闭"  CssClass="seaal" onclick="window.close();" id="Submit2" class="seaal"  />
</div>
</form>


<script type="text/javascript">

function setSelectValue(){
        var  roleSetFunc = thisform.setfunc.value; // 被选中菜单ID
		var roleLevel = thisform.level.value; // 菜单所属层次
		var roleFunc = roleSetFunc.split(",");
		var role_level = roleLevel.split(",");
		var rolefunc = ""; // 存储处理结果
		for (i = 0; i < roleFunc.length - 1; i++) {
			if (role_level[i] >= role_level[i + 1]) // 比较菜单层次，只保留叶子节点
				rolefunc = rolefunc + roleFunc[i] + ",";
		}		
		rolefunc = rolefunc + roleFunc[roleFunc.length - 1];				
        //alert(rolefunc);
        if(rolefunc!="")
        {
          win = window.opener;          
          win.form1.functions.value=rolefunc;
          win.opener=null;
          window.close();
         }
}


function addAgentRoleFunc(){
		thisform=document.form1;
		thisform.setfunc.value ="";
		thisform.level.value ="";
		for (i = 0; i < thisform.length; i++) {
		    if (thisform.elements[i].type == 'checkbox') {
		        if (thisform.elements[i].checked == true && "" != thisform.elements[i].id) {
		            thisform.setfunc.value = thisform.setfunc.value + thisform.elements[i].id + ",";
		            thisform.level.value = thisform.level.value + thisform.elements[i].value + ",";
		        }
		    }
		}
		//if(""==thisform.setfunc.value){
		//	alert("权限不能全部为空！");
		//	return false;
		//}
		setSelectValue();
		 return true;
	}
	
function selAll(mode,name,value){
	//alert(mode.value);
	//alert(name);
	//alert(value);
    thisform = document.form1;

	if(mode == "pad"){
		var j;
		var k;
		 for(i=0;i<thisform.length;i++){
		 	if(thisform.elements[i].id == name){
				ook=thisform.elements[i].checked;
				j=i;
				if(thisform.elements[i].checked){
					value1=thisform.elements[i].value;
					k=i;
				}
				break;
			}
		 }
	
		for(j=j+1;j<thisform.length;j+=1){

			if(thisform.elements[j].value <= value){
				break;
			}
			thisform.elements[j].checked =ook;
		}
		if(k>0){
			for(k=k-1;k>=0;k-=1){
				if(thisform.elements[k].value < value1){
					thisform.elements[k].checked =true;
					value1=thisform.elements[k].value;					
				}
			}
		}		
		
	}else{
		var j;
		
		 for(i=thisform.length-1;i>=0;i--){
		 	if(thisform.elements[i].id == name){
				if(thisform.elements[i].checked){
					value1=thisform.elements[i].value;
					j=i;
				}
			}
		 }
		if(j>0){
			for(j=j-1;j>=0;j-=1){
	
				if(thisform.elements[j].value < value1){
					thisform.elements[j].checked =true;
					value1=thisform.elements[j].value;					
				}
				
			}
		}
	}
}
/**
* 构造树，初值为0
*/
function tree(n) {
	var id = new Array("bar","pad","#","+");
    if(n == 0) { // 初始化变量
        n = 1;
        i = 0;
        s = "";
    } 
    s += "<table>";
    for(;i<tree_ar.length-1;i++) {
        var k = (n >= tree_ar[i + 1][0]) ? 0 : 1;
        
        var btn = "";

        if (k == 1) {
            btn = "onclick='openOrClose(this)'";
        }
        
     	if(tree_ar[i][3] == "1") {
     	    s += "<tr class='unnamed1' value=" + i + "><td ><span class='txt12Black' style ='cursor:pointer' " + btn + " id=" + id[k] + ">" + id[k + 2] + "</span></td><td><input type=checkbox checked value=" + tree_ar[i][0] + " id=" + tree_ar[i][2] + " name=" + tree_ar[i][1] + " onclick=selAll('" + id[k] + "','" + tree_ar[i][2] + "','" + tree_ar[i][0] + "')><span class='txt12Black'>" + tree_ar[i][1] + "</span></td></tr>";
	    }
	    else{
	        s += "<tr class='unnamed1' value=" + i + "><td ><span class='txt12Black' style ='cursor:pointer' " + btn + " id=" + id[k] + ">" + id[k + 2] + "</span></td><td><input type=checkbox value=" + tree_ar[i][0] + " id=" + tree_ar[i][2] + " name=" + tree_ar[i][1] + " onclick=selAll('" + id[k] + "','" + tree_ar[i][2] + "','" + tree_ar[i][0] + "')><span class='txt12Black'>" + tree_ar[i][1] + "</span></td></tr>"; 
        }
        if(n > tree_ar[i+1][0]) { // 若期望层次大于当前层次，结束本层次返回上一层次。

            s += "</td></tr></table>";
            return tree_ar[i+1][0];
        }
        if(n < tree_ar[i+1][0]) { // 若期望层次小于当前层次，递归进入下一层次。

            s += "<tr class='unnamed1' style=display:none  v=1><td ></td><td>";
            var m = tree(tree_ar[++i][0]);
            s += "</td></tr>";
            if(m < n) { // 当递归返回值小于当前层次期望值时，将产生连续的返回动作。

                s += "</table>";
                return m;
            }
        }
    }
    s += "</table>";
    return s;
}
</script>

<script language="jscript" type="text/jscript">
    function openOrClose(obj) {
        // 分枝节点的点击响应
        //v = this.parentElement.parentElement.parentElement.rows[this.parentElement.parentElement.rowIndex + 1].style;
        v = obj.parentElement.parentElement.parentElement.rows[obj.parentElement.parentElement.rowIndex + 1].style; ;

        //v = this.parentElement.parentElement.rows[this.rowIndex+1].style;
        if (v.display == "none") {
            v.display = "";
            obj.parentElement.parentElement.cells[0].innerHTML = "<span style ='cursor:pointer'  class='txt12Black' onclick='openOrClose(this)' id='pad'>-</span>";
        } else {

            v.display = "none";
            obj.parentElement.parentElement.cells[0].innerHTML = "<span style ='cursor:pointer' class='txt12Black' onclick='openOrClose(this)' id='pad'>+</span>";
        }
    }
    </script>


<script>
/*** 创建菜单 ***/
menus.innerHTML =tree(0);
</script>
</body>
</html>
